package main.computables;

import java.util.HashMap;
import java.util.Map;

public class CashedComputable<A,V> implements Computable<A,V>{
	
	private final Map<A,V> cashe;
	private final Computable<A, V> computable;
	
	public CashedComputable(Computable<A, V> computable){
		this.computable = computable;
		cashe = new HashMap<A,V>();
	}
	
	@Override
	public synchronized V compute(A arg) throws InterruptedException {
		V result = cashe.get(arg);
		if(result == null){
			result = computable.compute(arg);
			cashe.put(arg, result);
		}
		return result;
	}
	

}
